<?php
/**
 * Created by PhpStorm.
 * User: This
 * Date: 2018/11/27
 * Time: 20:55
 */
namespace app\admin\controller;

use app\admin\model\Menus;
use think\Db;

class Menu extends Base{
    public function _initialize(){
        $this->isLogin();
    }

    /**
     *查
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index(){
        $model = new Menus;
        $info = $model->getPage('','menu_id,menu_name,menu_group,menu_desc,menu_url,menu_status',5);
        $page = $info->render();
        $this->assign('site_title','导航列表-管理后台-百步园艺');
        $this->assign('menu',$info);
        $this->assign('page',$page);
        return $this->fetch('index');
    }

    /**
     * 增
     * @return mixed
     */
    public function add(){
        if(request()->isPost()){
            if($_POST){
                $data = input('post.');
                if(!isset($data['menu_name']) || !$data['menu_name']){
                    ajaxReturn(0,'导航不能为空!',0);
                }
                if(!isset($data['menu_url']) || !$data['menu_url']){
                    ajaxReturn(0,'链接不能为空!',0);
                }
                $menu = new Menus;
                $result = $menu->add($data);
                if($result){
                    ajaxReturn('/baibu/admin/Menu','添加成功!',1);
                }else{
                    ajaxReturn(0,'添加失败!',0);
                }
            }
        }
        $this->assign('site_title',"添加导航-管理后台-百步园艺");
        $this->assign('nav','添加导航');
        $this->assign('url','/baibu/admin/Menu/add');
        return $this->fetch('add');
    }

    /**
     * 删
     */
    public function del(){
        if(!$_GET){
            ajaxReturn(0,'参数错误!',-1);
        }
        if(gettype($_GET['menu_id']) == 'array'){
            $condition['menu_id'] = array('in',$_GET['menu_id']);
        }else{
            $condition['menu_id'] = $_GET['menu_id'];
        }
        $menu = new Menus;
        $result = $menu->del($condition);
        if($result){
            ajaxReturn($_GET['menu_id'],'删除成功!',1);
        }else{
            ajaxReturn(0,'删除失败!',0);
        }
    }

    /**
     * 改
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit(){
        $menu_id = input('menu_id');
        $menu = new Menus();
        $info = $menu->getOne(array('menu_id'=>$menu_id),'menu_id,menu_name,menu_desc,menu_url,menu_status');
        if(request()->isPost()){
            $data = $_POST;
            $condition['menu_id'] = $data['menu_id'];
            $result = $menu->edit($condition,$data);
            if($result){
                ajaxReturn('/baibu/admin/Menu','修改成功!',1);
            }else{
                ajaxReturn(0,'修改失败!',0);
            }
        }
        $this->assign('info',$info);
        $this->assign('url','/baibu/admin/Menu/edit');
        $this->assign('nav','修改导航');
        $this->assign('site_title','修改导航-管理后台-百步园艺');
        return $this->fetch('add');
    }

    /**
     * 修改状态
     */
    public function status(){
        if(!$_GET){
            ajaxReturn(0,'参数错误!',-1);
        }
        $update['menu_status'] = $_GET['menu_status'];
        $condition['menu_id'] = $_GET['menu_id'];
        $menu = new Menus;
        $result = $menu->edit($condition,$update);
        if($result){
            switch ($update['menu_status']){
                case 1:
                    $data['img'] = '/baibu/public/static/image/default/dui.png';
                    $data['name'] = "menu_id={$condition['menu_id']}&menu_status=0";
                    break;
                default :
                    $data['img'] = '/baibu/public/static/image/default/cuo.png';
                    $data['name'] = "menu_id={$condition['menu_id']}&menu_status=1";
                    break;
            }
            ajaxReturn($data,'操作成功!',1);
        }else{
            ajaxReturn(0,'操作失败!',0);
        }
    }
}